package com.example.server;

import java.io.DataOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.TaskResult;
import com.example.communication.ResultMarshaller;
import com.example.communication.ResultMarshallerJson;

/**
 * A serlvet which is responsible for responses for requests (concerting moves in the 8-puzzle game) made by
 * mobile clients.
 */
public class MobileResponseServlet extends HttpServlet {

    private ResultMarshaller marshaller;

    /**
     * Initializes the servlet
     */
    public void init() throws ServletException {
        marshaller = new ResultMarshallerJson();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TaskResult result = (TaskResult)request.getAttribute("result");
        response.setContentType("text/plain");
        DataOutputStream output = new DataOutputStream(response.getOutputStream());
        marshaller.marshal(output, result);
    }
}